<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
use Medoo\Medoo;

function write_log($content, $type="request")
{
    $rs=array('status'=>-1,'msg'=>"action is error");
    $log_dir=!empty(C('log_dir'))?C('log_dir'):'./log/';
    if (!is_dir($log_dir)) {
        if (!mkdir(iconv("UTF-8", "GBK", $log_dir), 0777, true)) {
            $rs['msg']='create dir error';
            return $rs;
        }
    }
    $path=$log_dir.$type.'_'.date("Y-m-d").'.log';
    $bool=file_put_contents($path, date("Y-m-d H:i:s"). " " . $content. "\r\n", FILE_APPEND | LOCK_EX);
    if (!empty($bool)) {
        $rs=array('status'=>1,'msg'=>"success");
    }
    return $rs;
}
//自定义数据库操作函数
function MDB($dbname='')
{
    $db = new Medoo(config('Meedo_DB'));
    return $db;
}

  //校验jwt权限API
 function verifyJwt($jwt)
 {
     $key = md5(C('JwtKey'));
     // JWT::$leeway = 3;
     $authInfo ;
     try {
         $authInfo = JWT::decode($jwt, $key, array('HS256'));
         return $authInfo;
     } catch (\Firebase\JWT\SignatureInvalidException $e) {
         // var_dump($e);
         $this->ec('token无效');
     } catch (\Firebase\JWT\ExpiredException $e) {
         // var_dump($e);
         $this->ec('token过期');
     } catch (Exception $e) {
         $this->ec($e);
     }
 }
